#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020/4/14 16:08|16:08
# @Author  : yangdingyi
# @File    : 445. 两数相加 II.2020-04-14
# @Software: PyCharm

# Definition for singly-linked list.


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        def ln2n(ln: ListNode) -> int:
            ret = 0
            while ln:
                ret = ret * 10 + ln.val
                ln = ln.next
            return ret
        a1 = ln2n(l1)
        a2 = ln2n(l2)
        aa = a1 + a2

        def n2ln(n: int) -> ListNode:
            ret = ListNode(n % 10)
            n //= 10
            while n:
                p = ListNode(n % 10)
                p.next = ret
                ret = p
                n //= 10
            return ret
        return n2ln(aa)


def display(x: ListNode):
    ln = []
    while x:
        ln.append(x.val)
        x = x.next
    ret = '->'.join(map(lambda x: f'{x}', ln))
    print(ret)
    return ret


def n2ln(n: int) -> ListNode:
    if n == 0:
        return ListNode(0)
    ret = None
    while n:
        if ret:
            p = ListNode(n % 10)
            p.next = ret
            ret = p
        else:
            ret = ListNode(n % 10)
        n //= 10
    return ret


solve = Solution()
l1 = n2ln(7243)
l2 = n2ln(564)

display(l1)
display(l2)
l3 = solve.addTwoNumbers(l1, l2)
display(l3)

